import { ref, onMounted } from "vue";
import { getTopCategoryAPI } from "@/apis/category";
import { useRoute } from "vue-router";
import { onBeforeRouteUpdate } from "vue-router";

export function useCategory() {
	const categoryData = ref({});
	const route = useRoute();
	async function getCategory(id = route.params.id) {
		const res: any = await getTopCategoryAPI(id as String);
		categoryData.value = res.result;
	}

	onMounted(() => {
		getCategory(route.params.id);
	});

	onBeforeRouteUpdate((to) => {
		console.log("路由变化");
		getCategory(to.params.id);
	});

	return { categoryData };
}

